#include<iostream>
#include<vector>
#include<set>
using namespace std;

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int m = board.size();
        int n = board[0].size();
        set<int> se;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == '.')
                    continue;
                else if (se.count(board[i][j])) {
                    return false;
                }
                se.insert(board[i][j]);
            }
            se.clear();
        }
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < m; i++) {
                if (board[i][j] == '.')
                    continue;
                else if (se.count(board[i][j])) {
                    return false;
                }
                se.insert(board[i][j]);
            }
            se.clear();
        }
        for (int a = 0; a < n; a += 3) {
            for (int b = 0; b < m; b += 3) {
                for (int i = a; i < a + 3; i++) {
                    for (int j = b; j < b + 3; j++) {
                        if (board[i][j] == '.')
                            continue;
                        else if (se.count(board[i][j])) {
                            return false;
                        }
                        se.insert(board[i][j]);
                    }
                }
                se.clear();
            }
        }

        return true;
    }
};